package com.caterina.activities;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MEventContract;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;

import contract.EventContract;
import controllers.ManagerTask.TaskType;


public class EventContractsActivity extends CActivity implements ModuleChangedListener{
	public EventContractsActivity(Container parent) {
		super("CONTRACTS_ACTIVITY_TITLE",parent);
	}
	@Override
	public void onStart() {
		this.createButtons();
		this.putTask(TaskType.EVENT_CONTRACT_GET_ALL, "");
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case EVENT_CONTRACT_GET_ALL:{
				this.contracts=(Collection<EventContract>) data;
				this.createViewer();
				break;
			}
			case EVENT_CONTRACT_SEND:{
				this.putTask(TaskType.EVENT_CONTRACT_GET_ALL, "");
				break;
			}
			case EVENT_CONTRACT_REVERT_TO_BUDGET:{
				this.putTask(TaskType.EVENT_CONTRACT_GET_ALL, "");
				break;
			}
		default:break;
		}
	}
	private void createButtons(){
		this.send=new CButton("EVENT_CONTRACT_SEND") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				putTask(TaskType.EVENT_CONTRACT_SEND, viewer.getSelectedContent().getContent());}
			private static final long serialVersionUID = 1L;
		};
		this.revert=new CButton("EVENT_CONTRACT_REVERT_TO_BUDGET") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				putTask(TaskType.EVENT_CONTRACT_REVERT_TO_BUDGET, viewer.getSelectedContent().getContent());}
			private static final long serialVersionUID = 1L;
		};
	}
	@Override
	public void onError(TaskType task, Exception e) {}
	@Override
	public void onModuleChange(Module<?> mod) {
		this.add(viewer);
		this.refresh();
	}
	private void createViewer(){
		
		this.createButtons();
		ArrayList<Module<?>> mods=new ArrayList<Module<?>> ();
		for(EventContract contr:this.contracts){
			MEventContract evCnt=new MEventContract(null, contr, "CONTRACT", "CONTRACT_DESC", ViewType.WRITABLE);
			evCnt.addWritableButton(this.revert, this.send);
			mods.add(evCnt);	
		}
		this.viewer=new ModCollectionViewer(null, mods, "CONTRACTS", "NO_CONTENT" , ViewType.WRITABLE,ModCollectionViewer.HIDE_ADD,ModCollectionViewer.HIDE_REMOVE);
		viewer.addModuleChangedListener(this);
		this.add(viewer);
	}
	
	private CButton revert;
	private ModCollectionViewer viewer;
	private CButton send;
	private Collection<EventContract>contracts;
	private static final long serialVersionUID = 1L;
}